package kr.co.graviti.controller.authoriry;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.co.graviti.common.util.CommonUtil;
import kr.co.graviti.common.util.CommonVariable;
import kr.co.graviti.common.util.PagingUtil;
import kr.co.graviti.controller.login.LoginController;
import kr.co.graviti.service.authority.AuthorityService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * 관리자 계쩡 추가/관리, 권한 추가/관리
 *
 */
@Controller
public class AuthorityController {
	
	private static final Logger logger = LoggerFactory.getLogger(AuthorityController.class);
	
	@Autowired
	private AuthorityService authorityService;
	
	/**
	 * @param request
	 * @param response
	 * @param currentPage
	 * @param currentGroup
	 * @param keyword
	 * @param searchword
	 * @return
	 */
	@RequestMapping(value="/authority/admin_list", method=RequestMethod.GET)
	public ModelAndView adminLIst(HttpServletRequest request
			, HttpServletResponse response
			, @RequestParam(value="page", required=true, defaultValue="1") int currentPage
			, @RequestParam(value="group", required=true, defaultValue="1") int currentGroup
			, @RequestParam(value="keyword", required=false) String keyword
			, @RequestParam(value="searchword", required=false) String searchword) {
		
		HashMap<String, Object> paging = new HashMap<String, Object>();
		paging.put("currentPage", currentPage);
		paging.put("currentGroup", currentGroup);
		paging.put("startNum", CommonVariable.PAGE_PER_NAVIGATOR * (currentPage - 1));
		paging.put("endNum", CommonVariable.PAGE_PER_NAVIGATOR);
		paging.put("keyword", keyword);
		paging.put("searchword", searchword);
		paging.put("lang", (String)request.getSession().getAttribute(CommonVariable.SESSION_LANG));
		int totArticle = this.authorityService.selectCountAdminList(paging);
		paging.put("totArticle", totArticle);
		
		List<HashMap<String, Object>> list = this.authorityService.selectAdminList(paging);
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName(new String("authority/admin_list"));
		mav.addObject("paging", new PagingUtil(paging).getPagingUtil());
		mav.addObject("totArticle", totArticle);
		mav.addObject("currentPage", currentPage);
		mav.addObject("page_per_navigator", CommonVariable.PAGE_PER_NAVIGATOR);
		mav.addObject("list", list);
		
		return mav;
	}
	
	/**
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/authority/admin_insert_form")
	public ModelAndView adminInsertForm(HttpServletRequest request
			, HttpServletResponse response
			, @RequestParam(value="userid", required=false) String userid
			, @RequestParam(value="mode", required=false) String mode) {
		
		HashMap<String, Object> param = new HashMap<String, Object>();
		String lang = (String)request.getSession().getAttribute(CommonVariable.SESSION_LANG);
		
		param.put("lang", lang);
		param.put("userid", userid);
		
		List<HashMap<String, Object>> menuList = authorityService.selectMenuList(param);
		HashMap<String, Object> adminInfo = null;
		List<HashMap<String, Object>> adminAuth = null;
		
		if(!CommonUtil.strNullChk(userid).equals("")){		//	수정모드
			adminInfo = authorityService.selectAdminInfoByUserid(param);
			adminAuth = authorityService.selectAdminAuthInfoByUserid(param);
		}
		
		ModelAndView mav = new ModelAndView();
		
		mav.setViewName(new String("authority/admin_insert_form"));
		mav.addObject("menuList", menuList);
		mav.addObject("adminInfo", adminInfo);
		mav.addObject("adminAuth", adminAuth);
		
		return mav;
	}
	
	@RequestMapping(value="/authority/individual_edit_form")
	public ModelAndView individualEditForm(HttpServletRequest request
			, HttpServletResponse response
			, @RequestParam(value="mode", required=false) String mode) {
		
		HashMap<String, Object> param = new HashMap<String, Object>();
		String lang = (String)request.getSession().getAttribute(CommonVariable.SESSION_LANG);
		String userid = (String)request.getSession().getAttribute(CommonVariable.SESSION_ADMIN_ID);
		
		param.put("lang", lang);
		param.put("userid", userid);

		List<HashMap<String, Object>> menuList = authorityService.selectMenuList(param);	//관리자 폼 메뉴권한 checkbox표현을 위한 리스트
		HashMap<String, Object> adminInfo = null;
		List<HashMap<String, Object>> adminAuth = null;		
		
		if (!CommonUtil.strNullChk(userid).equals("")) {	//수정모드
			adminInfo = authorityService.selectAdminInfoByUserid(param);
			adminAuth = authorityService.selectAdminAuthInfoByUserid(param);
		}
		
		ModelAndView mav = new ModelAndView();

		mav.setViewName(new String("authority/individual_edit_form"));
		mav.addObject("menuList", menuList);
		mav.addObject("adminInfo", adminInfo);
		mav.addObject("adminAuth", adminAuth);
		mav.addObject("mode", mode);
		
		return mav;
	}	
	
	/**
	 * 관리자 추가/수정
	 * @param request
	 * @param response
	 * @param userid		아이디
	 * @param passwd		비밀번호
	 * @param usernm		이름
	 * @param sosok			소속
	 * @param tel			전화번호
	 * @param email			이메일
	 * @param expln			비고
	 * @param auth_1		메뉴접근권한 아이디 목록
	 * @param auth_2		메뉴접근권한
	 * @param auth_3		메뉴접근권한
	 * @param auth_4		메뉴접근권한
	 * @param auth_5		메뉴접근권한
	 * @param auth			메뉴접근권한
	 * @param top_mn_cd		상단메뉴코드
	 * @return	ModelAndView
	 */
	@RequestMapping(value="/authority/insert_admin", method=RequestMethod.POST)
	public ModelAndView insertAdmin(HttpServletRequest request
			, HttpServletResponse response
			, @RequestParam(value="mode", required=true, defaultValue="") String mode
			, @RequestParam(value="userid", required=true, defaultValue="") String userid
			, @RequestParam(value="passwd", required=true, defaultValue="") String passwd
			, @RequestParam(value="usernm", required=true, defaultValue="") String usernm
			, @RequestParam(value="sosok", required=true, defaultValue="") String sosok
			, @RequestParam(value="tel", required=true, defaultValue="") String tel
			, @RequestParam(value="email", required=true, defaultValue="") String email
			, @RequestParam(value="expln", required=true, defaultValue="") String expln
			, @RequestParam(value="auth_1", required=true, defaultValue="") String auth_1
			, @RequestParam(value="auth_2", required=true, defaultValue="") String auth_2
			, @RequestParam(value="auth_3", required=true, defaultValue="") String auth_3
			, @RequestParam(value="auth_4", required=true, defaultValue="") String auth_4
			, @RequestParam(value="auth_5", required=true, defaultValue="") String auth_5
			, @RequestParam(value="auth_6", required=true, defaultValue="") String auth_6
			, @RequestParam(value="auth_7", required=true, defaultValue="") String auth_7
			, @RequestParam(value="auth_8", required=true, defaultValue="") String auth_8
			, @RequestParam(value="top_mn_cd", required=true, defaultValue="") String top_mn_cd
			, @RequestParam(value="mode01", required=true, defaultValue="") String mode01) {
		
		
		String [] arrAuth_1 = auth_1.split("\\,");
		String [] arrAuth_2 = auth_2.split("\\,");
		String [] arrAuth_3 = auth_3.split("\\,");
		String [] arrAuth_4 = auth_4.split("\\,");
		String [] arrAuth_5 = auth_5.split("\\,");
		String [] arrAuth_6 = auth_6.split("\\,");
		String [] arrAuth_7 = auth_7.split("\\,");
		String [] arrAuth_8 = auth_8.split("\\,");
		
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("userid", userid);
		param.put("passwd", passwd);
		param.put("usernm", usernm);
		param.put("sosok", sosok);
		param.put("tel", tel);
		param.put("email", email);
		param.put("expln", expln);
		String insert_userid = (String)request.getSession().getAttribute(CommonVariable.SESSION_ADMIN_ID);
		param.put("insert_userid", insert_userid);
		String lang = (String)request.getSession().getAttribute(CommonVariable.SESSION_LANG);
		param.put("lang", lang);
		
		if (mode.equals("insert")) {	//관리자 추가
			this.authorityService.insertAdminInfo(param);
		} else if (mode.equals("update")) {	//관리자 수정
			this.authorityService.deleteAdminAuthByUserid(param);
			this.authorityService.updateAdminInfoByUserid(param);
		} else if (mode.equals("delete")) {
			this.authorityService.deleteAdminAuthByUserid(param);
			this.authorityService.deleteAdminInfoByUserid(param);
		}
		
		insertArrayValue(arrAuth_1, param);
		insertArrayValue(arrAuth_2, param);
		insertArrayValue(arrAuth_3, param);
		insertArrayValue(arrAuth_4, param);
		insertArrayValue(arrAuth_5, param);
		insertArrayValue(arrAuth_6, param);
		insertArrayValue(arrAuth_7, param);
		insertArrayValue(arrAuth_8, param);
		
		String redirect = "";
		
		if(mode01.equals("individual")){
			redirect = new StringBuffer("redirect:/main/main.")
									.append(CommonVariable.EXTENSION)
									.append("?top_mn_cd=2").toString();
		} else {
			redirect = new StringBuffer("redirect:/authority/admin_list.")
									.append(CommonVariable.EXTENSION)
									.append("?top_mn_cd=2").toString();
		}
		
		return new ModelAndView(redirect);
	}

	/**
	 * @param arr	request checkbox value
	 * @param param insert HashMap 
	 */
	private void insertArrayValue(String[] arr,HashMap<String, Object> param) {
		for(int i = 0; i < arr.length; i++){
			if(!arr[i].equals("")){
				param.put("mn_cd", arr[i]);
				this.authorityService.insertAdminAuthority(param);
			}
		}
	}
	
	/**
	 * 관리자 아이디 중복 체크
	 * @param request
	 * @param response
	 * @param userid	관리자 아이디
	 * @return ModelAndView
	 */
	@RequestMapping(value="/authority/duplication_userid")
	public ModelAndView duplicationUserid(HttpServletRequest request
			, HttpServletResponse response
			, @RequestParam(value="userid") String userid) {
		
		int result = authorityService.selectDuplicationUserid(userid);
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("result", result);
		mav.setViewName(new String("authority/bin"));
		
		return mav;
	}
}
